package com.uum.blog.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uum.blog.pojo.entity.Category;
import com.uum.blog.pojo.entity.Tag;
import com.uum.blog.pojo.entity.Tag;
import com.uum.blog.pojo.query.TagPageQuery;
import com.uum.blog.service.TagService;
import com.uum.common.core.annotation.Operation;
import com.uum.common.core.enums.BusinessType;
import com.uum.common.core.result.Result;
import com.uum.web.util.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;


@Api(tags = "标签接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/v1/tag")
public class TagController {

    private final TagService tagService;

    @Operation(description = "查询标签列表",businessType = BusinessType.QUERY,isSaveResponseData = false)
    @ApiOperation(value = "标签列表分页")
    @GetMapping("page")
    public Result tagPage(TagPageQuery pageQuery) {
        IPage<Tag> page = tagService.page(new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize()),
                new LambdaQueryWrapper<Tag>().like(StrUtil.isNotBlank(pageQuery.getName()), Tag::getName, pageQuery.getName()));
        return Result.success(page);
    }

    @ApiOperation(value = "标签列表")
    @GetMapping("list")
    public Result tagList() {
        List<Tag> list = tagService.list(new LambdaQueryWrapper<Tag>().select(Tag::getId, Tag::getName));
        return Result.success(list);
    }

    @ApiOperation(value = "标签详情")
    @GetMapping("detail/{id}")
    public Result tagDetail(@PathVariable Long id) {
        Tag tag = tagService.getById(id);
        return Result.success(tag);
    }

    @Operation(description = "修改标签",businessType = BusinessType.UPDATE)
    @CacheEvict(cacheNames = "tag",key = "'list'")
    @ApiOperation(value = "修改标签")
    @PutMapping("update")
    public Result updateTag(@RequestBody Tag Tag) {
        boolean result = tagService.updateById(Tag);
        return Result.judge(result);
    }

    @Operation(description = "添加标签",businessType = BusinessType.INSERT)
    @CacheEvict(cacheNames = "tag",key = "'list'")
    @ApiOperation(value = "添加标签")
    @PostMapping("add")
    public Result addTag(@RequestBody Tag Tag) {
        Tag.setCreateUserId(JwtUtils.getUserId());
        boolean result = tagService.save(Tag);
        return Result.judge(result);
    }

    @Operation(description = "删除标签",businessType = BusinessType.DELETE)
    @CacheEvict(cacheNames = "tag",key = "'list'")
    @ApiOperation(value = "删除标签")
    @DeleteMapping("delete/{ids}")
    public Result deleteTag(@PathVariable String ids) {
        boolean result = tagService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.judge(result);
    }
}

